<template>
  <div class="login-container">
    <!-- 动画背景 -->
    <div class="background-animation">
      <div class="floating-shapes">
        <div class="shape shape-1"></div>
        <div class="shape shape-2"></div>
        <div class="shape shape-3"></div>
        <div class="shape shape-4"></div>
        <div class="shape shape-5"></div>
        <div class="shape shape-6"></div>
      </div>
      <div class="pulse-circles">
        <div class="pulse-circle pulse-1"></div>
        <div class="pulse-circle pulse-2"></div>
        <div class="pulse-circle pulse-3"></div>
      </div>
    </div>

    <!-- 登录表单 -->
    <div class="login-box">
      <div class="login-header">
        <div class="logo-container">
          <div class="logo-icon">
            <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
          </div>
        </div>
        <h2 class="login-title">血压管理系统</h2>
        <p class="login-subtitle">智能健康监测 · 专业医疗管理</p>
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @keyup.enter="handleLogin"
      >
        <el-form-item prop="userName">
          <div class="input-wrapper">
            <el-input
              v-model="loginForm.userName"
              placeholder="请输入用户名"
              size="large"
              prefix-icon="User"
              class="custom-input"
            />
          </div>
        </el-form-item>
        
        <el-form-item prop="password">
          <div class="input-wrapper">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              size="large"
              prefix-icon="Lock"
              show-password
              class="custom-input"
            />
          </div>
        </el-form-item>

        <el-form-item class="remember-me-item">
          <el-checkbox v-model="rememberMe" class="remember-checkbox">
            记住我
          </el-checkbox>
        </el-form-item>

        <el-form-item class="login-btn-item">
          <el-button
            type="primary"
            size="large"
            class="login-btn"
            :loading="loading"
            @click="handleLogin"
          >
            <span v-if="!loading">立即登录</span>
            <span v-else>登录中...</span>
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-footer">
        <p class="footer-text">© 2025 医院血压管理系统 · 守护健康每一天</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login } from '../api/auth'

const router = useRouter()
const loginFormRef = ref()
const loading = ref(false)
const rememberMe = ref(false)

const loginForm = reactive({
  userName: '',
  password: ''
})

const loginRules = {
  userName: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

// 从本地存储加载记住的用户信息
const loadRememberedUser = () => {
  const rememberedUser = localStorage.getItem('rememberedUser')
  if (rememberedUser) {
    try {
      const userData = JSON.parse(rememberedUser)
      loginForm.userName = userData.userName || ''
      loginForm.password = userData.password || ''
      rememberMe.value = true
    } catch (error) {
      console.error('加载记住的用户信息失败:', error)
    }
  }
}

// 保存或清除记住的用户信息
const handleRememberUser = () => {
  if (rememberMe.value) {
    const userData = {
      userName: loginForm.userName,
      password: loginForm.password
    }
    localStorage.setItem('rememberedUser', JSON.stringify(userData))
  } else {
    localStorage.removeItem('rememberedUser')
  }
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const response = await login(loginForm)
        
        // 接口返回格式为 { code: 200, msg: "成功", data: "token字符串" }
        if (response.code === 200) {
          // data直接就是token字符串，不是对象
          localStorage.setItem('token', response.data)
          
          // 处理记住我功能
          handleRememberUser()
          
          ElMessage.success('🎉 登录成功，欢迎使用血压管理系统！')
          router.push('/admin?menu=user-management')
        } else {
          ElMessage.error(response.msg || '登录失败')
        }
      } catch (error) {
        console.error('登录错误:', error)
        ElMessage.error(error.response?.data?.msg || error.message || '登录失败，请检查网络连接')
      } finally {
        loading.value = false
      }
    }
  })
}

// 组件挂载时加载记住的用户信息
onMounted(() => {
  loadRememberedUser()
})
</script>

<style scoped>
/* 主容器 */
.login-container {
  height: 100vh;
  position: relative;
  overflow: hidden;
  background: linear-gradient(135deg, #e8f4fd 0%, #c8e6f5 25%, #a8d8f0 50%, #88c9ea 75%, #68bbe5 100%);
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 动画背景 */
.background-animation {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

/* 浮动形状 */
.floating-shapes {
  position: absolute;
  width: 100%;
  height: 100%;
}

.shape {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.shape-1 {
  width: 80px;
  height: 80px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
  background: rgba(72, 187, 120, 0.15);
}

.shape-2 {
  width: 60px;
  height: 60px;
  top: 20%;
  right: 15%;
  animation-delay: 1s;
  background: rgba(66, 153, 225, 0.15);
}

.shape-3 {
  width: 100px;
  height: 100px;
  bottom: 20%;
  left: 15%;
  animation-delay: 2s;
  background: rgba(236, 72, 153, 0.15);
}

.shape-4 {
  width: 70px;
  height: 70px;
  bottom: 30%;
  right: 20%;
  animation-delay: 3s;
  background: rgba(245, 101, 101, 0.15);
}

.shape-5 {
  width: 50px;
  height: 50px;
  top: 50%;
  left: 5%;
  animation-delay: 4s;
  background: rgba(139, 92, 246, 0.15);
}

.shape-6 {
  width: 90px;
  height: 90px;
  top: 60%;
  right: 10%;
  animation-delay: 5s;
  background: rgba(34, 197, 94, 0.15);
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
    opacity: 0.7;
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
    opacity: 1;
  }
}

/* 脉冲圆圈 */
.pulse-circles {
  position: absolute;
  width: 100%;
  height: 100%;
}

.pulse-circle {
  position: absolute;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.3);
  animation: pulse 4s ease-in-out infinite;
}

.pulse-1 {
  width: 200px;
  height: 200px;
  top: 15%;
  left: 20%;
  animation-delay: 0s;
}

.pulse-2 {
  width: 150px;
  height: 150px;
  bottom: 25%;
  right: 25%;
  animation-delay: 1.5s;
}

.pulse-3 {
  width: 100px;
  height: 100px;
  top: 70%;
  left: 70%;
  animation-delay: 3s;
}

@keyframes pulse {
  0% {
    transform: scale(0.8);
    opacity: 1;
  }
  50% {
    transform: scale(1.2);
    opacity: 0.3;
  }
  100% {
    transform: scale(0.8);
    opacity: 1;
  }
}

/* 登录框 */
.login-box {
  width: 550px;
  padding: 50px 40px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  z-index: 2;
  animation: slideUp 0.8s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 登录头部 */
.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo-container {
  margin-bottom: 20px;
}

.logo-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto;
  background: linear-gradient(135deg, #4299e1 0%, #3182ce 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 10px 30px rgba(66, 153, 225, 0.3);
  animation: heartbeat 2s ease-in-out infinite;
}

.logo-icon svg {
  width: 40px;
  height: 40px;
  color: white;
}

@keyframes heartbeat {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
}

.login-title {
  color: #2d3748;
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 8px;
  background: linear-gradient(135deg, #2d3748 0%, #4a5568 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.login-subtitle {
  color: #718096;
  font-size: 14px;
  font-weight: 400;
  margin: 0;
}

/* 表单样式 */
.login-form {
  width: 100%;
}

.input-wrapper {
  position: relative;
  margin-bottom: 8px;
  width: 100%;
}

:deep(.custom-input) {
  width: 100%;
}

:deep(.custom-input .el-input__wrapper) {
  background: rgba(247, 250, 252, 0.8);
  border: 2px solid rgba(226, 232, 240, 0.8);
  border-radius: 12px;
  box-shadow: none;
  transition: all 0.3s ease;
  padding: 12px 16px;
}

:deep(.custom-input .el-input__wrapper:hover) {
  border-color: #4299e1;
  background: rgba(255, 255, 255, 0.9);
}

:deep(.custom-input.is-focus .el-input__wrapper) {
  border-color: #3182ce;
  background: rgba(255, 255, 255, 1);
  box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.1);
}

:deep(.custom-input .el-input__inner) {
  color: #2d3748;
  font-weight: 500;
}

:deep(.custom-input .el-input__inner::placeholder) {
  color: #a0aec0;
}

:deep(.custom-input .el-input__prefix) {
  color: #4299e1;
}

/* 记住我选项 */
.remember-me-item {
  margin-bottom: 30px;
}

.remember-checkbox {
  color: #4a5568;
  font-weight: 500;
}

:deep(.remember-checkbox .el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #4299e1;
  border-color: #4299e1;
}

:deep(.remember-checkbox .el-checkbox__input.is-checked + .el-checkbox__label) {
  color: #4299e1;
}

/* 登录按钮 */
.login-btn-item {
  margin-bottom: 0;
}

.login-btn {
  width: 100%;
  height: 50px;
  background: linear-gradient(135deg, #4299e1 0%, #3182ce 100%);
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  box-shadow: 0 8px 25px rgba(66, 153, 225, 0.3);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 35px rgba(66, 153, 225, 0.4);
  background: linear-gradient(135deg, #3182ce 0%, #2c5aa0 100%);
}

.login-btn:active {
  transform: translateY(0);
}

.login-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.login-btn:hover::before {
  left: 100%;
}

/* 页脚 */
.login-footer {
  text-align: center;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid rgba(226, 232, 240, 0.6);
}

.footer-text {
  color: #a0aec0;
  font-size: 12px;
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-box {
    width: 90%;
    max-width: 400px;
    padding: 40px 30px;
    margin: 20px;
  }
  
  .login-title {
    font-size: 24px;
  }
  
  .logo-icon {
    width: 60px;
    height: 60px;
  }
  
  .logo-icon svg {
    width: 30px;
    height: 30px;
  }
  
  .shape {
    display: none;
  }
}

@media (max-width: 480px) {
  .login-box {
    padding: 30px 20px;
  }
  
  .pulse-circle {
    display: none;
  }
}
</style>